import pandas as pd
import numpy as np
from jieba import analyse

df = pd.read_csv('static/data/house_info_pre.csv')
df_liner = pd.read_csv('static/data/liner_Regression.csv')
df_keyword = pd.read_csv('static/data/keyword.csv')


def mean_unit_price():
    df_mean_unit_price = df.loc[:, ['house_type', 'unit_price', 'floor_type']]
    floor_type_list = df_mean_unit_price.groupby('floor_type').count().index.tolist()
    house_type_list = df_mean_unit_price.groupby('house_type').count().index.tolist()
    data_list = [{'name': i, 'value': [
        df_mean_unit_price[(df_mean_unit_price['floor_type'] == i) & (df_mean_unit_price['house_type'] == j)] for
        j in house_type_list]} for i in floor_type_list]
    value = [{
        'name': i['name'],
        'value': [round(j['unit_price'].mean()) if len(j) > 0 else 0
                  for j in i['value']]} for i in data_list]
    return {
        'name': house_type_list,
        'value': value
    }


def diff_orient_ratio():
    df_orient = df.loc[:, ['title', 'orient']]
    df_orient['orient'] = df_orient['orient'].apply(lambda x: x[0])
    diff_orient_ratio_data_list = df_orient.groupby('orient').count().reset_index().values.tolist()
    return {
        'data': [{'name': i[0], 'value': i[1]} for i in diff_orient_ratio_data_list]
    }


def scatter_data():
    scatter_data_list = df[['area', 'unit_price']].values.tolist()
    return {
        'scatter_list': scatter_data_list,
        'line_list': df_liner.values.tolist()
    }


def heatmap_data():
    df_heatmap = df[['floor_type', 'house_type', 'title']]
    floor_type_list = df_heatmap.groupby('floor_type').count().index.tolist()
    house_type_list = df_heatmap.groupby('house_type').count().index.tolist()
    data = [[i, j, int(df_heatmap[
                           (df_heatmap['floor_type'] == floor_type_list[i]) & (
                                   df_heatmap['house_type'] == house_type_list[j])].count()[
                           'title'])] for j in range(len(house_type_list)) for i in range(len(floor_type_list))]
    return {
        'data': data,
        'floor_type_list': floor_type_list,
        'house_type_list': house_type_list
    }


def title_word_frequency_top50():
    data = [{'name': i[0], 'value': i[1]} for i in df_keyword.head(50).values.tolist()]
    return {
        'data': data
    }


def funnel_data():
    df_total_price_under_300 = df[df['total_price'] < 300]
    df_total_price_under_300['orient'] = df_total_price_under_300['orient'].apply(lambda x: x[0])
    df_orient_south = df_total_price_under_300[df_total_price_under_300['orient'] == '南']
    df_area_between_100_and_150 = df_orient_south[(df_orient_south['area'] > 100) & (df_orient_south['area'] < 150)]
    df_high_floors = df_area_between_100_and_150[df_area_between_100_and_150['floor_type'] == '高楼层']
    return {
        'max': len(df),
        'data': [
            {
                'name': '高楼层',
                'value': len(df_high_floors)
            },
            {
                'name': '面积在100-150平',
                'value': len(df_area_between_100_and_150)
            },
            {
                'name': '房屋朝南',
                'value': len(df_orient_south)
            },
            {
                'name': '总价低于300万',
                'value': len(df_total_price_under_300)
            },
        ]
    }
